import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;

public class main {

	private static HashMap<String, Country> datas = new HashMap<String, Country>();

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// Get CO2 Data:
		Scanner sc = null;
		try {
			sc = new Scanner(
					new File(
							"/Users/korbinianpauli/Documents/workspace_eclipse/DataParserAndConverter/src/CO2.tsv"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		sc.useDelimiter("[\\n;]"); // this means whitespace or comma
		while (sc.hasNext()) {
			String next = sc.next();

			// get the data:
			String[] splitted = next.split("\\t");
			String countryId = splitted[0].split(",")[1];

			Country cObj = new Country();
			cObj.id = countryId;

			for (int i = 1; i <= 12; i++) {
				int value = -1;
				try {
					value = Integer.parseInt(splitted[i].replaceAll(" ", ""));
				} catch (NumberFormatException e) {

				}
				cObj.setCO2ForYear(i + 1999, value);
			}

			datas.put(countryId, cObj);

		}

		// Get Energy Consumption Data:
		try {
			sc = new Scanner(
					new File(
							"/Users/korbinianpauli/Documents/workspace_eclipse/DataParserAndConverter/src/energy_consumption.tsv"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		sc.useDelimiter("[\\n;]"); // this means whitespace or comma
		while (sc.hasNext()) {
			String next = sc.next();

			// get the data:
			String[] splitted = next.split("\\t");
			String countryId = splitted[0].split(",")[3];

			Country cObj = datas.get(countryId);

			if (cObj != null) {
				for (int i = 1; i <= 12; i++) {
					int value = -1;
					try {
						value = Integer.parseInt(splitted[i].replaceAll(" ", ""));
					} catch (NumberFormatException e) {
						System.out.println("Error: X" + splitted[i].replaceAll(" ", "") + "X");

					}
					cObj.setEnergyForYear(i + 1999, value);
				}

			}

		}
		//printDataForMotionChartTime();
		//printDataForCandlestickChart();
		//printDataForPieChart(2010);
		printDataForTreeMap(2010);
	}
	
	public static void printDataForTreeMap(int year) {
		Iterator entries2 = datas.entrySet().iterator();
        while (entries2.hasNext()) {
            Map.Entry entry = (Map.Entry)entries2.next();
            String key = (String)entry.getKey();
            Country cObj = (Country)entry.getValue();
                       
            System.out.println("['" + cObj.id + "', 'Europe', "  + cObj.getCO2ForYear(year) + ", " + cObj.getEnergyForYear(year) + "],");                        	
     
        }
		
		
	}
	
	public static void printDataForPieChart(int year) {
		Iterator entries2 = datas.entrySet().iterator();
        while (entries2.hasNext()) {
            Map.Entry entry = (Map.Entry)entries2.next();
            String key = (String)entry.getKey();
            Country cObj = (Country)entry.getValue();
                        
            System.out.println("['" + cObj.id + "', "  + cObj.getCO2ForYear(year) + "],");                        	
     
        }
		
		
	}
	
//	public static void printDataForCandlestickChart() {
//		Iterator entries2 = datas.entrySet().iterator();
//		String output = "";
//        while (entries2.hasNext()) {
//            Map.Entry entry = (Map.Entry)entries2.next();
//            String key = (String)entry.getKey();
//            Country cObj = (Country)entry.getValue();
//            
//            output += "['" + cObj.id + "', ";
//            for (int i = 2000; i <= 2011; i++) {
//            	output += cObj.getCO2ForYear(i);
//            	if (i < 2011) {
//            		output += ", ";
//            	} else {
//            		output += "],\n";
//            	}
//            }
//        }
//		System.out.println(output);
//	}
	
	
	public static void printDataForMotionChartTime() {
		Iterator entries2 = datas.entrySet().iterator();
        while (entries2.hasNext()) {
            Map.Entry entry = (Map.Entry)entries2.next();
            String key = (String)entry.getKey();
            Country cObj = (Country)entry.getValue();
            
            for (int i = 2000; i <= 2011; i++) {
            	System.out.println("['" + cObj.id + "', " + i + ", " + cObj.getEnergyForYear(i) + ", " + cObj.getCO2ForYear(i) + "],");                        	
            }
        }
		
	}
	

}
